package com.example.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Constants;
import com.example.common.HttpStatusEnum;
import com.example.config.ConfigTableValueConfig;
import com.example.entity.ResponseData;
import com.example.entity.VoSelfDrData;
import com.example.pojo.Doctor;
import com.example.mapper.DoctorMapper;
import com.example.pojo.Enterprise;
import com.example.util.FileUtil;
import org.springframework.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * (Doctor)表服务实现类
 *
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/01/17
 * @time: 23:13
 */
@Service
public class DoctorService {
    /**
     * mapper对象
     */
    @Resource
    private DoctorMapper doctorMapper;
    // 系统配置参数
    @Resource
    private ConfigTableValueConfig configValue;

    /**
     * 分页查询
     *
     * @param currentPage
     * @param pageSize
     * @param doctor      筛选条件
     * @return
     */
    public ResponseData getByPage(Integer currentPage, Integer pageSize, Doctor doctor) {
        LambdaQueryWrapper<Doctor> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.hasText(doctor.getName()), Doctor::getName, doctor.getName());
        lqw.eq(!Arrays.asList(null, -1).contains(doctor.getDeleted()), Doctor::getDeleted, doctor.getDeleted());
        IPage ipage = new Page(currentPage, pageSize);
        doctorMapper.selectPage(ipage, lqw);
        //若当前页码大于总页面数
        if (currentPage > ipage.getPages()) {
            ipage = new Page(ipage.getPages(), pageSize);
            doctorMapper.selectPage(ipage, lqw);
        }
        if (ipage.getTotal() <= 0) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), ipage);
    }

    /**
     * 根据ID查询信息
     *
     * @param id 主键
     * @return 单条数据
     */
    public ResponseData findById(Long id) {
        Doctor doctor = doctorMapper.selectById(id);
        if (doctor == null || doctor.getId() == null) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        //返回结果
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), doctor);
    }

    /**
     * 新增数据
     *
     * @param doctor 实体
     * @return 新增结果
     */
    public ResponseData add(Doctor doctor) {
        //插入
        if (doctorMapper.insert(doctor) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "新增失败，请稍后再试");
    }

    /**
     * 更新数据
     *
     * @param doctor 实体
     * @return 更新结果
     */
    public ResponseData update(Doctor doctor) {
        //更新
        if (doctorMapper.updateById(doctor) > 0) {
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "修改失败，请稍后再试");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return
     */
    public ResponseData delete(Long id) {
        if (doctorMapper.deleteById(id) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 批量删除
     *
     * @param idList
     * @return
     */
    public ResponseData deleteBatch(List<Long> idList) {
        if (doctorMapper.deleteBatchIds(idList) > 0) {
            //返回成功响应
            return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
        }
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), "删除失败，请稍后再试");
    }

    /**
     * 获取所有有效医生信息
     *
     * @return
     */
    public ResponseData getAllValidDr() {
        // 查询
        LambdaQueryWrapper<Doctor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doctor::getDeleted, 0);
        List<Doctor> doctors = doctorMapper.selectList(lqw);
        // 返回结果
        if (doctors == null || doctors.isEmpty()) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), doctors);
    }

    /**
     * 获取医生信息设置页数据
     *
     * @param uid
     * @return
     */
    public ResponseData getSelfDrDataByUid(Long uid) {
        // 查询
        VoSelfDrData selfDrData = doctorMapper.selectSelfDrDataByUid(uid);
        // 返回结果
        if (selfDrData == null || selfDrData.getCode() == null) {
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage(), selfDrData);
    }

    /**
     * 上传签名
     */
    public ResponseData uploadSignByUid(MultipartFile file, Long uid) {
        // 查询原数据
        LambdaQueryWrapper<Doctor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Doctor::getUid, uid);
        Doctor doctor = doctorMapper.selectOne(lqw);
        // 替换
        if (doctor != null) {
            String oldPic = doctor.getSign();
            String newPic = FileUtil.storeFile(file, configValue.getDrSignStorePath(), false);
            if (StringUtils.hasText(newPic)) {
                // 更新
                doctor.setSign(configValue.getDrSignNetUrl() + Constants.FORWARD_SLASH + newPic);
                doctorMapper.updateById(doctor);
                // 删除原来的,如果有 (不删除原来的，由于诊断和药单记录都保存了之前的签名链接)
                // if (StringUtils.hasText(oldPic)) {
                //     FileUtil.deleteFile(configValue.getDrSignStorePath() + File.separator
                //             , oldPic.replace(configValue.getDrSignNetUrl() + Constants.FORWARD_SLASH, ""));
                // }
                // 返回成功标识
                return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), HttpStatusEnum.SUCCESS.getMessage());
            }
        } else {
            // 数据不存在标识
            return new ResponseData(HttpStatusEnum.DATA_NOT_EXIST.getCode(), HttpStatusEnum.DATA_NOT_EXIST.getMessage());
        }
        // 更新失败标识
        return new ResponseData(HttpStatusEnum.ERROR.getCode(), HttpStatusEnum.ERROR.getMessage());
    }
}
